﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace GameServer.Managers
{
    class virtualItem
    {
        private int _ID = 0;
        public int _Damage = 0;
        private int[] prices;
        private int _Cash;
        private int _Level = 1;
        private String _WeaponCode = "";
        private String _ItemName;

        private bool _isValidWeapon = false;
        private bool _isPremium = false;
        private bool validShopItem = false;
        private bool _isValidPx = false;
        private bool _isCostumeItem = false;
        private bool _isSkin = false;

        public virtualItem(int ID, int Damage, string WeaponCode, string WeaponName, int[] shopPrices, bool PremiumOnly, bool ValidWeapon, bool ValidShopItem, bool PxItem, int Level, int Cash, bool isCostumeItem, bool isSkin)
        {
            this._ID = ID;
            this._Damage = Damage;
            this._WeaponCode = WeaponCode;
            this._ItemName = WeaponName;
            this._isValidWeapon = ValidWeapon;
            this.prices = shopPrices;
            this._isPremium = PremiumOnly;
            this.validShopItem = ValidShopItem;
            this._isValidPx = PxItem;
            this._isCostumeItem = isCostumeItem;
            this._isSkin = isSkin;
            this._Level = Level;
            this._Cash = Cash;
        }

        public int Level { get { return _Level; } }
        public int ID { get { return _ID; } }
        public int Damage { get { return _Damage; } }
        public String WeaponCode { get { return _WeaponCode; } }
        public bool isValidWeapon { get { return _isValidWeapon; } }
        public int getPrice(int X) { return prices[X]; }
        public int Cash { get { return _Cash; } }
        public bool isPremium { get { return _isPremium; } }
        public bool isValidShopItem { get { return validShopItem; } }
        public bool isValidPXItem { get { return _isValidPx; } }
        public bool isCostumeItem { get { return _isCostumeItem; } }
        public bool isSkin { get { return _isSkin; } }
        public String Name { get { return _ItemName; } }
    }

    class ItemManager
    {
        private static Hashtable _Weapons = new Hashtable();

        public static bool Load()
        {
            try
            {
                Log.WriteLine("Loading Weapon information into the memory..");
                _Weapons.Clear();

                int[] tableIDs = DB.runReadColumn("SELECT id from items", 0, null);

                for (int I = 0; I < tableIDs.Length; I++)
                {
                    string[] Data = DB.runReadRow("SELECT wid, strcode, damage, price1, price2, price3, price4, premiumOnly, validWeapon, validShopItem, name, validPX, level, cash, validCostume, isSkin FROM items WHERE id=" + tableIDs[I]);
                    if (_Weapons.ContainsKey(Data[1].ToLower()) == false)
                    {
                        int[] _tPrice = new int[4];
                        for (int J = 0; J < 4; J++)
                        {
                            _tPrice[J] = int.Parse(Data[3 + J]);
                        }
                        _Weapons.Add(Data[1].ToLower(), new virtualItem(int.Parse(Data[0]), int.Parse(Data[2]), Data[1].ToLower(), Data[10], _tPrice, Data[7].Equals("1"), Data[8].Equals("1"), Data[9].Equals("1"), Data[11].Equals("1"), int.Parse(Data[12]), int.Parse(Data[13]), Data[14].Equals("1"), Data[15].Equals("1")));
                    }
                    else
                    {
                        Log.WriteError("Duplicated item(" + tableIDs[I] + "-" + Data[1] + " in weapon table.");
                    }
                }

                Log.WriteLine("Loaded the information of " + _Weapons.Count + " weapons into the memory.");
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteError("Failed to load all weapons into the memory!");
                Log.WriteError("Error: " + ex.Message);
            }

            return false;
        }

        public static int getDamage(string ItemCode)
        {
            string Item = ItemCode.ToLower();
            if (_Weapons.ContainsKey(Item))
            {
                virtualItem _Weapon = (virtualItem)_Weapons[Item];
                return _Weapon.Damage;
            }
            return 0;
        }

        public static bool isValidWeapon(string ItemCode)
        {
            string Item = ItemCode.ToLower();
            if (_Weapons.ContainsKey(Item))
            {
                virtualItem _Weapon = (virtualItem)_Weapons[Item];
                return _Weapon.isValidWeapon;
            }

            return false;
        }

        public static virtualItem getItem(string ItemCode)
        {
            string Item = ItemCode.ToLower();
            if (_Weapons.ContainsKey(Item))
            {
                return (virtualItem)_Weapons[Item];
            }

            return null;
        }

        public static virtualItem getWeapon(int ID)
        {
            if (ID > 0)
            {
                foreach (virtualItem Item in ((Hashtable)_Weapons.Clone()).Values)
                {
                    if (Item.ID == ID) return Item;
                }
            }

            return null;
        }
    }
}
